﻿#include "stdafx.h"
#include "ScriptMaple.h"
#include "thread_control.h"
#include "script.h"
#include "GameAppDlg.h"

bool ChangeLine(CString strLine) {
	//查找迅游窗口是否存在 不存在则启动迅游
	long x, y, x1, y1;
	long hwnd = g_dm->FindWindowW(L"XunYouMainClass_4B32", L"迅游国际版");
	if (hwnd == 0) {
		g_dm->RunApp(L"D:\\Program Files (x86)\\XunYou_Intl\\xunyou.exe", 0);
		Sleep(5000);
		for (size_t i = 0; i < 15; i++)
		{
			g_dm->FindPic(0, 0, 1280, 960, L"xytag.bmp", L"050505", 0.95, 0, &x, &y);
			if (x > 0) {
				hwnd = g_dm->FindWindowW(L"XunYouMainClass_4B32", L"迅游国际版");
				break;
			}
			Sleep(1000);
		}
	}
	g_dm->SetWindowState(hwnd, 12);
	g_dm->UnBindWindow();
	int ret = g_dm->BindWindowEx(hwnd, L"normal", L"normal", L"normal", L"normal", 0);
	g_dm->SetDict(2, L"line.txt");
	g_dm->UseDict(2);
	if (ret > 0) {
		for (size_t i = 0; i < 5; i++)
		{
			//g_dm->BindWindowEx(hwnd, L"normal", L"normal", L"normal", L"normal", 0);
			//识别迅游窗口内线路是否是需要连接的线路
			CString str = g_dm->Ocr(52, 444, 207, 470, L"bbbbbb-555555", 1);
			//CString str = g_dm->Ocr(x3 - 188, y3 + 10, x3, y3 + 200, L"bbbbbb-555555", 1);
			//g_main_cwnd->AppendText(L"识别数据:" + str);

			if (str == strLine) {//如果迅游窗口内的线路为需要的线路则直接启动线路
				g_dm->FindPic(0, 0, 1280, 960, L"startnet.bmp", L"050505", 0.95, 0, &x, &y);
				if (x > 0) {//启动成功后返回
					return true;
				}
				g_dm->FindPic(0, 0, 1280, 960, L"nostartnet.bmp", L"050505", 0.95, 0, &x, &y);
				if (x > 0) {//找到线路后启动线路
					g_dm->MoveTo(x, y);
					g_dm->LeftClick();
					Sleep(2000);
				}
			}
			else {
				Sleep(1000);
				g_dm->UnBindWindow();
				g_dm->SetDict(2, L"line.txt");
				g_dm->UseDict(2);
				//查找所需的线路并点击选中线路
				g_dm->FindPic(0, 0, 1280, 960, L"startnet.bmp", L"050505", 0.95, 0, &x1, &y1);
				if (x1 > 0) {//未识别到线路已经启动线路则先关闭线路
					g_main_cwnd->AppendText(L"网络已连接关闭网络");
					g_dm->MoveTo(x1 + 33, y1 - 32);
					g_dm->LeftClick();
					Sleep(5000);
				}
				g_dm->FindPic(0, 0, 1280, 960, L"nostartnet.bmp", L"050505", 0.95, 0, &x1, &y1);
				if (x1 > 0) {
					g_dm->FindPic(0, 0, 1280, 960, L"netlist.bmp", L"050505", 0.9, 0, &x, &y);
					if (x == -1) {
						g_main_cwnd->AppendText(L"网络未连接");
						g_dm->MoveTo(x1 + 67, y1 + 85);
						g_dm->LeftClick();
						Sleep(2000);
					}
				}
				//查找线路列表是否开启
				g_dm->FindPic(0, 0, 1280, 960, L"netlist.bmp", L"050505", 0.9, 0, &x, &y);
				if (x > 0) {
					g_dm->MoveTo(x - 148, y + 39);
					g_dm->LeftClick();
					Sleep(3000);
					g_dm->MoveTo(x, y + 88);
					g_dm->LeftClick();
					Sleep(2000);
					long x3 = x, y3 = y;
					for (size_t i = 0; i < 100; i++)
					{
						str = g_dm->Ocr(x3 - 188, y3 + 10, x3, y3 + 200, L"bbbbbb-555555", 1);
						//CString str = g_dm->Ocr(x3 - 188, y3 + 10, x3, y3 + 200, L"bbbbbb-555555", 1);
						g_main_cwnd->AppendText(L"识别数据2:" + str);
						g_dm->FindStrFast(x3 - 188, y3 + 10, x3, y3 + 200, strLine, L"bbbbbb-555555", 1, &x, &y);
						if (x > 0) {//找到线路并点击
							g_dm->MoveTo(x, y);
							g_dm->LeftClick();
							Sleep(2000);
							g_dm->FindPic(0, 0, 1280, 960, L"nostartnet.bmp", L"050505", 0.95, 0, &x1, &y1);
							if (x1 > 0) {
								g_dm->MoveTo(x1, y1);
								g_dm->LeftClick();
								Sleep(10000);
								g_dm->BindWindowEx(hwnd, L"normal", L"normal", L"normal", L"normal", 0);
								//识别迅游窗口内线路是否是需要连接的线路
								str = g_dm->Ocr(52, 444, 207, 470, L"bbbbbb-555555", 1);
								//CString str = g_dm->Ocr(x3 - 188, y3 + 10, x3, y3 + 200, L"bbbbbb-555555", 1);
								g_main_cwnd->AppendText(L"识别数据1:" + str);
								if (str == strLine)
								{
									g_dm->SetWindowState(hwnd, 2);
									return true;
								}
							}
							break;
						}
						g_dm->WheelDown();
						Sleep(700);
					}//未找到关闭线路列表重新打开
					g_dm->FindPic(0, 0, 1280, 960, L"startnet.bmp", L"050505", 0.95, 0, &x1, &y1);
					if (x1 > 0) {//未识别到线路已经启动线路则先关闭线路
						g_dm->MoveTo(x1 + 33, y1 - 32);
						g_dm->LeftClick();
						Sleep(5000);
					}
					g_dm->FindPic(0, 0, 1280, 960, L"nostartnet.bmp", L"050505", 0.95, 0, &x, &y);
					g_dm->MoveTo(x + 67, y + 85);
					g_dm->LeftClick();
				}
				else {
					g_dm->FindPic(0, 0, 1280, 960, L"nostartnet.bmp", L"050505", 0.95, 0, &x, &y);
					if (x > 0) {
						g_dm->MoveTo(x + 67, y + 85);
						g_dm->LeftClick();
						Sleep(1000);
					}
				}
			}
		}
	}
	//查找是否已经启动线路，启动则先关闭线路




	//检查线路是否关闭，关闭后打开线路列表

	//查找线路找到后选择线路

	//选中线路后查询迅游窗口是否选中正确的线路如果正确则点击开启加速器 

	//查找线路是否启动成功，成功则返回

	return false;
}




